using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Suadiadanh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddropdownlist();
            LoadData();
            
        }
    }
    private void loaddropdownlist()
    {
        dr_tenddiem.DataSource = tbDiadiemBUS.GetAll();
        dr_tenddiem.DataTextField = tbDiadiemCollumn.tendiadiem.ToString();
        dr_tenddiem.DataValueField = tbDiadiemCollumn.madiadiem.ToString();
        dr_tenddiem.DataBind();
        dr_tenddiem.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_madd .Text = id.ToString();

            tbDiadanh  ddanh = tbDiadanhBUS .GetByID(id);
            txttenddanh.Text = ddanh .Tendiadanh ;
            dr_tenddiem.SelectedValue = ddanh .Madiadiem .ToString();
         
            txt_mota .Text = ddanh .Motadiadanh ;
      
            img_hmh.ImageUrl = "~/Hinhanh/Hinh/" +ddanh .Hinhdiadanh ;
          

        }
        catch
        {
            Response.Redirect("~/Admin/Danhsachdiadanh.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbDiadanh  ddanh = tbDiadanhBUS .GetByID(id);
            
            ddanh .Tendiadanh  = txttenddanh.Text;
           
            ddanh .Madiadiem  = int.Parse(dr_tenddiem.SelectedValue);
         
            ddanh .Motadiadanh  = txt_mota .Text;
         
            if (fu_hmh.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_hmh.FileName;
                fu_hmh.SaveAs(Server.MapPath("~/Hinhanh/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Hinhanh/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Hinhanh/Hinh/"), filename);
                // xoa hinh cu
                if (ddanh .Hinhdiadanh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + ddanh .Hinhdiadanh ));
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" +ddanh .Hinhdiadanh ));
                }
                ddanh .Hinhdiadanh  = filename;
            }
            tbDiadanhBUS .Update(ddanh );
            Response.Redirect("~/Admin/Danhsachdiadanh.aspx");
        }


        
    
   
}
